export const paramParsing = (data, dataItem) => {
    var params = data.split('&');
    // console.log( params );
    var obj = {};
    var arr = [];
    params.forEach(ele => {
        var key = ele.split('=')[0];
        var value = ele.split('=')[1];
        obj[key] = value;

    })
    for (var key in obj) {
        // console.log( obj[key],  obj[key].search("{"),  obj[key].search("}") );
        let a = obj[key].substring(obj[key].indexOf("{") + 1, obj[key].indexOf("}"));
        // console.log( a );
        obj[key] = dataItem[a];

        arr.push(key + '=' + obj[key])
    }
    return arr.join('&');
}

export const parameterParsing = (data, form) => {
    var params = data.split('&');
    var obj = {};
    params.forEach(ele => {
        var key = ele.split('=')[0];
        var value = ele.split('=')[1];
        console.log(value);
        if (value != "undefined") {
            obj[key] = value;
        }

    })
    // form.value = obj;
    return obj
}

export const parsingURLs = (apiObjName) => {
    if (apiObjName == undefined) { return }
    // ;;code=#{code}
    let arr = apiObjName.match(/#/g);
    var dataArr;
    if (arr.length == 1) {
        let a = apiObjName.indexOf('{',);
        let b = apiObjName.indexOf('}');
        dataArr = [apiObjName.substring(a + 1, b)];
    } else if (arr.length == 2) {
        let a = apiObjName.indexOf('{',);
        let a2 = apiObjName.indexOf('{', a + 1);
        let b = apiObjName.indexOf('}');
        let b2 = apiObjName.indexOf('}', b + 1);
        dataArr = [apiObjName.substring(a + 1, b), apiObjName.substring(a2 + 1, b2)];
        return dataArr
    }
    return dataArr
}
export const parsingJson = (selectData, item) => {
    const obj = JSON.parse(item.paramTemplate);
    var params = {};
    for (var key in obj) {
        if (obj[key].indexOf("#") == -1) {
            params[key] = obj[key]
        }
        else {
            let cs = parsingURLs(obj[key]);
            if (cs.length == 1) { //
                if (item.chooseRows == 2) { //多条数据
                    // if( item.target == 'dialog'|| item.chooseRows == 1){
                    // selectData.forEach( ele => { params[key] = ele[cs[0]] })
                    // }else{
                    //     if( item.chooseRows == 1){
                    //         selectData.forEach( ele => { params[key] = ele[cs[0]] })
                    // }else{
                    //     let arr = [];
                    //     selectData.forEach( ele => { 
                    //         arr.push( ele[cs[0]] );
                    //         params[key] = arr;
                    //     })
                    //     }
                    if (selectData.length) {
                        let arr = [];
                        selectData.forEach(ele => {
                            arr.push(ele[cs[0]]);
                            params[key] = arr;
                        })
                    }
                    else {
                        params[key] = selectData[cs[0]];
                    }
                }
                else {
                    if (selectData.length) {
                        selectData.forEach(ele => {
                            params[key] = ele[cs[0]]
                        })
                    }
                    else {
                        params[key] = selectData[cs[0]];
                    }

                }
            }
        }
    }
    if ('param' in params) {
        if (isJSON(params.param)) {
            for (let key in JSON.parse(params.param)) {
                params[key] = JSON.parse(params.param)[key]
            }
            delete params.param
        }
    }
    return params
}
const isJSON = (str) => {
    try {
        if (typeof JSON.parse(str) == "object") {
            return true;
        }
    } catch (e) {
    }
    return false;
}



